package AnalysisRpm;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern; 


public class rpmOpera {
	
	public static String reMovSpace(String  str)
	{
		String res;
		Pattern p = Pattern.compile("\\s");
		Matcher m = p.matcher(str);
		res = m.replaceAll("\\\\ ");
		
		return res;
	}
	
	public static String combineCmd_qpi(String file) 
	{
		String res = reMovSpace(file);
		System.out.println("lynn* " + file);
		res = "source ~/.bash_profile && rpm -qpi " + file;
		return res;
	}
	
	public static String combineCmd_qpR(String file) 
	{
		String res = reMovSpace(file);
		System.out.println("lynn* " + file);
		res = "source ~/.bash_profile && rpm -qpR " + file;
		return res;
	}
	
	public static String exeCmd(String commandStr) 
    {
		StringBuilder sb = new StringBuilder();
        try {
            ProcessBuilder builder = new ProcessBuilder("/bin/bash","-c",commandStr);
            System.out.println("lynn# " + commandStr);
            builder.redirectErrorStream(true);
            Process p = builder.start();
            int code = p.waitFor();
            InputStream is =  p.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            while(true){
                String line = br.readLine();
                if(line == null){
                    break;
                }
                 
                if(line.trim().length()>0){
                    sb.append(line);
                    sb.append("\n");
                }
            }
            br.close();
            isr.close();
            is.close();
            System.out.println(">>>>>>>>>>>>>>>>>>>>>");
            System.out.println(code);//
            System.out.println(sb.toString());
            System.out.println("<<<<<<<<<<<<<<<<<<<<<");
             
            InputStream eis = p.getErrorStream();
            InputStreamReader eisr = new InputStreamReader(eis);
            BufferedReader ebr = new BufferedReader(eisr);
            StringBuilder esb = new StringBuilder();
            while(true){
                String eline = ebr.readLine();
                if(eline==null){
                    break;
                }
                if(eline.trim().length()>0){
                    esb.append(eline);
                    esb.append("\n");
                }
            }
            ebr.close();
            eisr.close();
            eis.close();
             
            System.out.println(esb.toString());
            
             
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return sb.toString();
    }
 
	public static List<File> searchFiles(File folder, String keyword)
	{
		List<File> result = new ArrayList<File>();
		
		if(folder.isFile())
		{
			result.add(folder);
		}
		
		File[] subFolders = folder.listFiles(new FileFilter()
				{
					public boolean accept(File file)
					{
						if(file.isDirectory())
						{
							return true;
						}
						
						if(file.getName().toLowerCase().contains(keyword))
						{
							return true;
						}
						return false;
					}
				}
				
			);
		
		if(subFolders != null)
		{
			for(File file : subFolders)
			{
				if(file.isFile())
				{
					result.add(file);
				}
				else
				{
					result.addAll(searchFiles(file, keyword));
				}
			}
		}
		
		return result;
	}

}


